package com.tgjorgoski.utils;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;

public class IconResource
{

	private static ImageIcon emptyIcon;
	private static String ICON_PATH = "com/tgjorgoski/sidekicknotes/images/";
	
	static {
		emptyIcon = new ImageIcon() {
			public int getIconWidth() { return 16; }
			public int getIconHeight() { return 16; }
			public void paintIcon(Component c, Graphics g, int x, int y) { }
		};
	}

	public static String getResourceName(String iconName) {
		if (iconName.indexOf('.') == -1)
			return ICON_PATH + iconName + ".png";
		else
			return ICON_PATH + iconName;
	}
	
	public static URL getIconURL(String iconName) {
		URL iconURL = null;
		String fullPath = getResourceName(iconName);
		iconURL = IconResource.class.getClassLoader().getResource(fullPath);
		return iconURL;
	}

	public static ImageIcon getIconResource(String iconName) {
		URL iconURL = getIconURL( iconName );
		if (iconURL != null)
			return new ImageIcon( iconURL );
		else
			return emptyIcon;
	}
	
	public static BufferedImage getCompatibleImage(ImageIcon image) {
        GraphicsConfiguration configuration = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
        BufferedImage compatibleImage = configuration.createCompatibleImage(image.getIconWidth(), image.getIconHeight(), Transparency.TRANSLUCENT);
        Graphics g = compatibleImage.getGraphics();
       	g.drawImage(image.getImage(), 0, 0, null);
        g.dispose();
        return compatibleImage;
	}
	
	public static ImageIcon getOverlayedIcon(ImageIcon background, ImageIcon overlay, int overlayXpan, int overlayYpan) {
		int width = Math.max(background.getIconWidth(), overlayXpan + overlay.getIconWidth());
		int height = Math.max(background.getIconHeight(), overlayYpan + overlay.getIconHeight());
        GraphicsConfiguration configuration = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
        BufferedImage compatibleImage = configuration.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
        Graphics g = compatibleImage.getGraphics();
       	g.drawImage(background.getImage(), 0, 0, null);
		g.drawImage(overlay.getImage(), overlayXpan, overlayYpan, null);
		g.dispose();
		ImageIcon icon = new ImageIcon(compatibleImage);
		return icon;
	}
}
